package longrise.phone.com.itssnew.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.longrise.android.Global;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.OnClick;
import longrise.phone.com.itssnew.R;
import longrise.phone.com.itssnew.utils.CommonUtil;


public class SettingActivity extends BaseActivity {
    @Bind(R.id.et_serverUrl)
    EditText etServerUrl;
    @Bind(R.id.et_updateUrl)
    EditText etUpdateUrl;
    @Bind(R.id.et_versionName)
    EditText etVersionName;
    @Bind(R.id.et_macAddress)
    EditText etMacAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private void initView() {
        etServerUrl.setText(Global.getInstance().getServerUrl());
        etUpdateUrl.setText(Global.getInstance().getUpdateUrl());
        etVersionName.setText(Global.getInstance().getVersionName());
        etMacAddress.setText(CommonUtil.getLocalMacAddress());
    }

    @Override
    public int bindLayoutId() {
        return R.layout.activity_setting;
    }

    @OnClick({R.id.cancle, R.id.sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancle:
                finish();
                break;
            case R.id.sure:
                Global.getInstance().setServerUrl(replaceBlank(etServerUrl.getText().toString().trim()));
                Global.getInstance().setUpdateUrl(replaceBlank(etUpdateUrl.getText().toString().trim()));
                finish();
                break;
        }
    }

    private String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }
}
